<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>电影中心</title>
    <%- include common %>
        <style>
            .mv-item {
                text-align: center;
                margin-top: 20px;
                box-sizing: border-box;
                padding: 0 10px;
            }

            .box {
                border-radius: 10px;
                padding: 5px;
                transition: .3s;
                padding-top: 20px;
            }

            .box:hover {
                box-shadow: 0 10px 15px rgba(0, 0, 0, 0.07);
                transform: translateY(-2px);
            }

            .cl {
                display: flex;
                align-items: center;
                justify-content: space-between;
            }

            .search {
                margin-top: 5px;
                background: #fff;
                border-radius: 16px 16px 0 0;
            }

            .search .search_form {
                line-height: 1;
                width: 318px;
                height: 28px;
                background: #f0f0f0;
                border: 1px solid #f0f0f0;
                border-radius: 15px;
                position: relative;
            }

            .search .search_form input {
                border: 0;
                color: #3a3a3a;
                width: 285px;
                text-indent: 15px;
                height: 30px;
                line-height: 30px;
                background: transparent;
                outline: none;
            }

            .search .search_form span {
                cursor: pointer;
            }

            .hide {
                display: none;
            }

            .sxy {
                background: #6699ff;
                color: #fefefe;
                border-radius: 5px;
            }

            #sort span {
                padding: 5px;
                cursor: pointer;
            }

            em {
                font-style: normal;
            }
        </style>
</head>

<body>
    <%- include('head',{title:'movie',username:username })%>
        <div class="container">
            <div class="cl">
                <h2 class="text-success">电影中心</h2>
                <div class="w_r">
                    <div class="search">
                        <div class="search_form">
                            <input type="text" placeholder="熔炉" id="search_con">
                            <span id="search" class="glyphicon glyphicon-search" aria-hidden="true"></span>
                        </div>
                    </div>
                </div>
                <div id="sort">
                    <span>排序：</span>
                    <span class="sxy" id="default">综合</span>
                    <span id="year"><em>年份</em><i class="glyphicon glyphicon-arrow-up"></i><i
                            class="glyphicon glyphicon-arrow-down hide"></i></span>
                    <span id="score"><em>评分</em><i class="glyphicon glyphicon-arrow-up"></i><i
                            class="glyphicon glyphicon-arrow-down hide"></i></span>
                </div>
            </div>
            <div class="container-fluid">
                <div class="row" id="render">
                    <% result.forEach((item,i)=>{ %>
                        <div class="col-md-3 mv-item">
                            <a href="/mvDetail?mid=<%-  item.id   %>">
                                <div class="box">
                                    <div style="width:100%;height:220px;">
                                        <img style="height:220px;" src="<%- item.images.large.replace(/img7/,'img3') %>"
                                            alt="<%- item.original_title %>">
                                    </div>
                                    <div>
                                        <h4 class="text-success">
                                            <%- item.title%>
                                        </h4>
                                        <p>年份: <%- item.year%>
                                        </p>
                                        <p>评分: <%- item.rating.average%>
                                        </p>
                                        <p>分类: <%- item.genres %>
                                        </p>
                                    </div>
                                </div>
                            </a>
                        </div>
                        <% }) %>
                </div>
            </div>
        </div>
        <%- include foot %>
</body>
<script>
    var index = -1;
    $('#sort').on('click', 'span', function () {
        var html = '';
        var year = 'year';
        var score = 'score';
        init();
        if ($(this).attr('id') == 'default') {
            $('.sxy').removeClass('sxy');
            $(this).addClass('sxy');
            $.ajax({
                url: '/movie',
                type: 'GET',
                data: {
                    def: 'def'
                },
                success({ result }) {
                    $(result).each((_, item) => {
                        html += `<div class="col-md-3 mv-item">
                            <a href='/mvDetail?mid=${item.id}'>
                                <div class="box">
                                    <div style="width:100%;height:220px;">
                                        <img style="height:220px;" src='${item.images.large.replace(/img7/, 'img3')}' alt=' ${item.original_title}'>
                                    </div>
                                    <div>
                                        <h4 class="text-success">
                                            ${item.title}
                                        </h4>
                                        <p>年份: ${item.year}
                                        </p>
                                        <p>评分: ${item.rating.average}
                                        </p>
                                        <p>分类: ${item.genres}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        </div>`
                    })
                    $('#render').html(html);
                }
            })
        }
        if ($(this).attr('id') == 'year') {
            $('.sxy').removeClass('sxy');
            $(this).addClass('sxy');
            $(this).find('i').each((_, item) => {
                if ($(item).hasClass('hide')) {
                    $(item).removeClass('hide');
                }
            })
            var up = $(this).find('.glyphicon-arrow-up');
            var down = $(this).find('.glyphicon-arrow-down');
            if (index == 1) {
                down.addClass('hide');
            } else {
                up.addClass('hide');
            }
            $.ajax({
                url: '/movie',
                type: 'GET',
                data: {
                    index,
                    year
                },
                success({ result }) {
                    $(result).each((_, item) => {
                        html += `<div class="col-md-3 mv-item">
                            <a href='/mvDetail?mid=${item.id}'>
                                <div class="box">
                                    <div style="width:100%;height:220px;">
                                        <img style="height:220px;" src='${item.images.large.replace(/img7/, 'img3')}' alt=' ${item.original_title}'>
                                    </div>
                                    <div>
                                        <h4 class="text-success">
                                            ${item.title}
                                        </h4>
                                        <p>年份: ${item.year}
                                        </p>
                                        <p>评分: ${item.rating.average}
                                        </p>
                                        <p>分类: ${item.genres}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        </div>`
                    })
                    $('#render').html(html);
                }
            })
        }
        if ($(this).attr('id') == 'score') {
            $('.sxy').removeClass('sxy');
            $(this).addClass('sxy');
            $(this).find('i').each((_, item) => {
                if ($(item).hasClass('hide')) {
                    $(item).removeClass('hide');
                }
            })
            var up = $(this).find('.glyphicon-arrow-up');
            var down = $(this).find('.glyphicon-arrow-down');
            if (index == 1) {
                down.addClass('hide');
            } else {
                up.addClass('hide');
            }
            $.ajax({
                url: '/movie',
                type: 'GET',
                data: {
                    index,
                    score
                },
                success({ result }) {
                    $(result).each((_, item) => {
                        html += `<div class="col-md-3 mv-item">
                            <a href='/mvDetail?mid=${item.id}'>
                                <div class="box">
                                    <div style="width:100%;height:220px;">
                                        <img style="height:220px;" src='${item.images.large.replace(/img7/, 'img3')}' alt=' ${item.original_title}'>
                                    </div>
                                    <div>
                                        <h4 class="text-success">
                                            ${item.title}
                                        </h4>
                                        <p>年份: ${item.year}
                                        </p>
                                        <p>评分: ${item.rating.average}
                                        </p>
                                        <p>分类: ${item.genres}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        </div>`
                    })
                    $('#render').html(html);
                }
            })
        }
    })

    function init() {
        $('i').each((_, item) => {
            if ($(item).hasClass('hide')) {
                $(item).removeClass('hide');
            }
        })
        $('.glyphicon-arrow-down').each((_, item) => {
            $(item).addClass('hide');
        })
        index = index * -1;
    }

    $('#search').click(function () {
        var str = $('#search_con').val();
        var html = '';
        $.ajax({
            url: '/search',
            type: 'GET',
            data: {
                str
            },
            dataType: 'json',
            success({ result, type,msg }) {
                if (type) {
                    $(result).each((_, item) => {
                        html += `<div class="col-md-3 mv-item">
                            <a href='/mvDetail?mid=${item.id}'>
                                <div class="box">
                                    <div style="width:100%;height:220px;">
                                        <img style="height:220px;" src='${item.images.large.replace(/img7/, 'img3')}' alt=' ${item.original_title}'>
                                    </div>
                                    <div>
                                        <h4 class="text-success">
                                            ${item.title}
                                        </h4>
                                        <p>年份: ${item.year}
                                        </p>
                                        <p>评分: ${item.rating.average}
                                        </p>
                                        <p>分类: ${item.genres}
                                        </p>
                                    </div>
                                </div>
                            </a>
                        </div>`
                    })
                    $('#render').html(html);
                }else{
                    alert(msg);
                }
            }
        })
    })

</script>

</html>